﻿using RimWorld;
using RimWorld.Planet;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using Verse;
using Verse.AI.Group;

namespace SZ.Mech
{
    public class IncidentWorker_Dragon : IncidentWorker
    {
        protected override bool CanFireNowSub(IncidentParms parms)
        {
            return SZ_DefOf.SZ_HeavenForgedCelestiall != null;
        }


        protected override bool TryExecuteWorker(IncidentParms parms)
        {
            Map map = (Map)parms.target;
            float points = Mathf.Max(parms.points, 600f);
            Faction FactionHeavenForged = Find.FactionManager.FirstFactionOfDef(SZ_DefOf.SZ_HeavenForgedCelestiall);
            ///集群任务，突击殖民地
            Lord lord = LordMaker.MakeNewLord(FactionHeavenForged, new LordJob_AssaultColony(), map);





            ///不能用建筑物
            //ThingDef shipPartDef = def.mechClusterBuilding;
            IntVec3 intVec =  DropCellFinder.RandomDropSpot(map);
            //DropCellFinder.TryFindRaidDropCenterClose(out IntVec3 intVec,map);
            //IntVec3 intVec =
            Pawn coreThing = PawnGenerator.GeneratePawn(def.pawnKind, FactionHeavenForged);
                //intVec = FindDropPodLocation(map, (IntVec3 spot) => CanPlaceAt(spot));
                //bool CanPlaceAt(IntVec3 loc)
                //{
                //    //占用的区域
                //    //CellRect cellRect = GenAdj.OccupiedRect(loc, Rot4.North, shipPartDef.Size);
                //    //if (loc.Fogged(map) || !cellRect.InBounds(map))
                //    //{
                //    //    return false;
                //    //}

                //    //if (!DropCellFinder.SkyfallerCanLandAt(loc, map,IntVec2.One))
                //    //{
                //    //    return false;
                //    //}
                //    //foreach (IntVec3 item2 in cellRect)
                //    //{
                //    //    RoofDef roof = item2.GetRoof(map);
                //    //    if (roof != null && roof.isNatural)
                //    //    {
                //    //        return false;
                //    //    }
                //    //}

                //    return GenConstruct.CanBuildOnTerrain(shipPartDef, loc, map, Rot4.North);
                //}
                //if (intVec == IntVec3.Invalid)
                //{
                //    return false;
                //}
                //coreThing = (Pawn)ThingMaker.MakeThing(shipPartDef);
                //SubMechCluster subMechCluster = coreThing.GetComp<SubMechCluster>();
                //if (subMechCluster != null)
                //{
                //    subMechCluster.IncidentParms = parms;
                //}
                lord.AddPawn(coreThing);
                //coreThing.SetFaction(FactionHeavenForged);
                //lord.AddBuilding(coreThing);

            ///随机普通单位
            List<PawnKindDef> statueThingDefs = new List<PawnKindDef>();
            statueThingDefs.Add(SZ_DefOf.SZ_HeavenForgedRanger);
            statueThingDefs.Add(SZ_DefOf.SZ_HeavenForgedMarksman);
            statueThingDefs.Add(SZ_DefOf.SZ_HeavenForgedPositionDestroyer);
            statueThingDefs.Add(SZ_DefOf.SZ_HeavenForgedThunder);
            float usePoint = points;

            ///使用投射的单位
            List<Thing> mechThings = new List<Thing>();
            while (usePoint > 0)
            {
                var useDef = statueThingDefs[UnityEngine.Random.Range(0, statueThingDefs.Count)];

                //Pawn mech = (Pawn)ThingMaker.MakeThing(useDef);
                Pawn mech = PawnGenerator.GeneratePawn(useDef, FactionHeavenForged);
                //mech.SetFaction(FactionHeavenForged);
                mechThings.Add(mech);
                //CompPawnSpawnOnWakeup compPawnSpawnOnWakeup = statuebuild.TryGetComp<CompPawnSpawnOnWakeup>();
                usePoint -= mech.kindDef.combatPower;
                //CompCanBeDormant compCanBeDormant = statuebuild.TryGetComp<CompCanBeDormant>();
                //compCanBeDormant.ToSleep();
                lord.AddPawn(mech);
            }
            //ActiveDropPod activeDropPod = (ActiveDropPod)ThingMaker.MakeThing(SZ_DragonTaihao_DefOf.SZ_DragonFlyA);//行动吊舱

            //var active = ThingMaker.MakeThing(SZ_DragonTaihao_DefOf.SZ_DragonFlyA);
            //var drop = ThingMaker.MakeThing(SZ_DragonTaihao_DefOf.DropPodIncomingDragonFlyA);


            ActiveDropPodInfo info = new ActiveDropPodInfo();
            info.innerContainer.TryAdd(coreThing);

            //MiaoUtility.DropTravelingTransportPods(pods, cell, mapParent.Map, activeDropPodDef, dropPodIncomingDef);



            //FlyShipLeaving obj = (FlyShipLeaving)SkyfallerMaker.MakeSkyfaller(dragonFlyExtension.dropPodLeavingFly, activeDropPod);//飞行中的效果


            MiaoUtility.MakeDropPodAt(intVec, map, info, SZ_DragonTaihao_DefOf.SZ_DragonFlyA, SZ_DragonTaihao_DefOf.DropPodIncomingDragonFlyA, FactionHeavenForged);
            DropPodUtility.DropThingsNear(intVec, map, mechThings);



            

            //MiaoUtility.MakeDropPodAt(intVec, map, info,SZ_DefOf.SZ_DragonFlyA, SZ_DefOf.DropPodIncomingDragonFlyA);


        
            //if (coreThing != null)
            //{
            //    GenSpawn.Spawn(SkyfallerMaker.MakeSkyfaller(ThingDefOf.CrashedShipPartIncoming, coreThing), intVec, map);//高空坠落物
            //    SendStandardLetter(parms, coreThing);
            //}
            //else
            //{
            //    SendStandardLetter(parms, statueThings[0]);
            //}
            SendStandardLetter(parms,mechThings);//会提示强制转换，先不管
            return true;
        }

        private static IntVec3 FindDropPodLocation(Map map, Predicate<IntVec3> validator)
        {
            for (int i = 0; i < 200; i++)
            {
                IntVec3 intVec = RCellFinder.FindSiegePositionFrom(DropCellFinder.FindRaidDropCenterDistant(map, allowRoofed: true), map, allowRoofed: true);
                if (validator(intVec))
                {
                    return intVec;
                }
            }
            return IntVec3.Invalid;
        }

    }
}
